﻿using Newtonsoft.Json.Linq;

namespace CommandLib
{
    /// <summary>
    /// 应用配制文件读取类
    /// </summary>
    public sealed class AppConfig
    {
        private static readonly string baseDir = AppContext.BaseDirectory;
        /// <summary>
        /// 配制文件操作
        /// </summary>
        private static JObject Configuration;
        /// <summary>
        /// 配制文件初始化，默认为appsettings.json
        /// </summary>
        /// <param name="filename"></param>
        /// <exception cref="Exception"></exception>
        public static void Init(string filename = "appsettings.json")
        {

            var fileroot = baseDir + filename;
            if (File.Exists(fileroot))
            {
                var jsonstr = File.ReadAllText(fileroot);
                Configuration = JObject.Parse(jsonstr);
            }
            else
            {
                throw new Exception("没有找到配置文件");
            }

        }

        /// <summary>
        /// 连接配制
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string ConnectionStrings(String key)
        {

            if (Configuration["ConnectionStrings"] != null)
            {
                return Configuration["ConnectionStrings"][key]?.ToString();
            }
            else
            {
                return string.Empty;
            }


        }
        /// <summary>
        /// 应用配制
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string AppSetting(String key)
        {

            if (Configuration["AppSettings"] != null)
            {
                return Configuration["AppSettings"][key]?.ToString();
            }
            else
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// 获取指定节点的值，用“:”分割
        /// </summary>
        /// <param name="key">获取键值</param>
        /// <returns></returns>
        public static string GetSection(string key)
        {
            var result = string.Empty;
            if (!key.IsEmpty()&& Configuration!=null)
            {
                var selectNode = new JObject();
                var keys = key.Split(':');
                if (keys.Length > 0)
                {
                    selectNode = Configuration;                   
                    for (int i = 0; i < keys.Length; i++)
                    {
                        if (i == keys.Length - 1)
                        {
                            result = selectNode[keys[i]]?.ToString();
                        }
                        else
                        {
                            selectNode = JObject.FromObject(Configuration[keys[i]]);
                        }
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// 获取多级节点的值，用“:”分割
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="key">获取键值</param>
        /// <returns></returns>
        public static T GetSection<T>(string key)
        {
            var result = default(T);
            if (!key.IsEmpty())
            {
                try
                {
                    var rdata = GetSection(key);
                    result = rdata.JsonToObject<T>();
                }
                catch (Exception)
                {
                    throw;
                }
              
            }
            return result;
        }
    }
}
